//
//  StatusCell.swift
//  weibo-01
//
//  Created by 韩家奇 on 15/11/9.
//  Copyright © 2015年 hanjiaqi. All rights reserved.
//

import UIKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    var viewModle: StatusViewModel? {
        didSet {
            topView.viewModle = viewModle
            contentLabel.text = viewModle?.status.text
            //测试修改配图视图高度 - 实际开发中如果动态的修改约束可能会导致行高计算错误
            //设置配图模型 - 设置视图模型后 配图视图会计算大小
            pictureview.viewModle = viewModle
            pictureview.snp_updateConstraints { (make) -> Void in
//                make.height.equalTo(random() % 4 * 90)
                make.height.equalTo(pictureview.bounds.height)
                //直接设置宽度
                make.width.equalTo(pictureview.bounds.width)
                
                //根据配图的数量,决定配图视图的顶部间距
//                let offset = viewModle?.thumbnailUrls?.count > 0 ? StatusCellMargin : 0
//                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
                make.width.equalTo(pictureview.bounds.width)
            }
        }
    }
    func rowHeight(vm:StatusViewModel) -> CGFloat {
        //1.记录视图模型 -> 回调uong上面的didset 设置内容以及跟新的约束
        viewModle = vm
        //2.轻质更新所有越苏 --- 所有空间的frame都会被计算正确
        contentView.layoutIfNeeded()
        //3. 返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    // MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 懒加载空间
/// 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
 /// 微博正文
     lazy var contentLabel: UILabel = UILabel(title: "微博正文",
        fontSize: 15,
        color: UIColor.darkGrayColor(),
        screenInset: StatusCellMargin)
    //配图视图
     lazy var pictureview: StatusPictureView = StatusPictureView()
 /// 底部视图
     lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}
// MARK: - 设置界面
extension StatusCell {
     func setupUI() {
        //1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureview)
        contentView.addSubview(bottomView)
        //2.自动布局
        //1>顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2*StatusCellMargin + StatusCellIconWidth)
        }
        
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
//        //3.配图视图
//        pictureview.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//        }
         //3>底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureview.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
    


        }

    }
}